package logic

import (
	"context"
	"fmt"
	"goods/crud"

	"goods/goods"
	"goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodListLogic {
	return &GetGoodListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetGoodListLogic) GetGoodList(in *goods.GetGoodListRequest) (*goods.GetGoodListResponse, error) {
	goodMod := crud.NewGoodsModel(crud.Client)
	goodInfos, err := goodMod.FindList(l.ctx, in.Offset, in.Limit)
	if err != nil {
		return nil, err
	}
	res := []*goods.Good{}
	for _, val := range goodInfos {
		res = append(res, &goods.Good{
			ID:      fmt.Sprintf("%v", val.Id),
			Name:    val.Name,
			Content: val.Content,
			Price:   val.Price,
		})
	}
	return &goods.GetGoodListResponse{
		Infos: res,
	}, nil
}
